package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.HospitalStatusBean;
import com.hxkcloud.nurse.beans.LineListBean;
import com.hxkcloud.nurse.beans.MonitorKindBean;
import com.hxkcloud.nurse.beans.PiontListBean;
import com.hxkcloud.nurse.beans.PointBean;
import com.hxkcloud.nurse.dborm.MonitorKindDao;
import com.hxkcloud.nurse.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * Created by ray_lei on 2016/7/6.
 */
public class Chart1Adapter extends RecyclerView.Adapter<ViewHolder> {


    private Context mContext;
    private List<LineListBean> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ResultSelectedListener listener;

    List<HospitalStatusBean> beans;

    String patientId;
    private boolean isPatient;//患者可以录入，护士不可以

    MonitorKindDao kindDao;
    private int screenWidth;

    public Chart1Adapter(Context mContext, List<LineListBean> datas,
                         RecyclerView recyclerview, ResultSelectedListener listener, String patientId, boolean isPatient, int width) {
        this.mContext = mContext;
        this.datas = datas;
        this.recyclerview = recyclerview;
        this.listener = listener;
        this.patientId = patientId;
        this.isPatient = isPatient;
        this.screenWidth = width;
        mInflater = LayoutInflater.from(mContext);
        kindDao = new MonitorKindDao(mContext);

    }

    public void setData(List<LineListBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {


        Holder mHolder = (Holder) holder;
        mHolder.titleTV.setText(datas.get(position).getName());
        mHolder.dateTV.setText(datas.get(position).getDate());
        if (datas.get(position).getLineList().get(0).getPointList().size() == 0) {
            mHolder.nodataLayout.setVisibility(View.VISIBLE);
        } else {
            mHolder.nodataLayout.setVisibility(View.GONE);
        }

        MonitorKindBean kindBean = kindDao.getKind(datas.get(position).getKindId());
        String isKindBean = "3";//1.血糖2.血压3.其他
        if (kindBean != null) {

            if (kindBean.getTestList() != null) {
                if (kindBean.getTestList().size() > 0) {//血糖
                    mHolder.nodataIV.setImageResource(R.mipmap.monitor_no_data_purpli);
                    mHolder.nodataTV.setTextColor(mContext.getResources().getColor(R.color.purpli));
                    mHolder.inputBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_purpli_corner));
                    isKindBean = "1";
                    mHolder.abnormalLayout.setVisibility(View.VISIBLE);
                    mHolder.highLayout.setVisibility(View.GONE);
                    mHolder.lowLayout.setVisibility(View.GONE);
                    mHolder.iconIV.setImageResource(R.mipmap.monitor_blood);
                }
            }
            if (kindBean.getValueList() != null) {
                if (kindBean.getValueList().size() > 0) {//血压
                    mHolder.nodataIV.setImageResource(R.mipmap.monitor_no_data);
                    mHolder.nodataTV.setTextColor(mContext.getResources().getColor(R.color.blue_text));
                    mHolder.inputBtn.setBackground(mContext.getResources().getDrawable(R.drawable.bg_btn_blue_corner));
                    isKindBean = "2";
                    mHolder.abnormalLayout.setVisibility(View.VISIBLE);
                    mHolder.highLayout.setVisibility(View.VISIBLE);
                    mHolder.lowLayout.setVisibility(View.VISIBLE);
                    mHolder.iconIV.setImageResource(R.mipmap.monitor_pressure);
                }
            }

        }

        RelativeLayout.LayoutParams lp_chart = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, screenWidth * 4 / 5);
        lp_chart.addRule(RelativeLayout.BELOW, R.id.layout_item_titlelayout);
        lp_chart.leftMargin = ImageUtil.dip2px(5, mContext);
        mHolder.chartLayout.setLayoutParams(lp_chart);

        generateData(datas.get(position).getMinValue(), datas.get(position).getMaxValue(), datas.get(position).getLineList(), mHolder.chartLayout, isKindBean);
        mHolder.chartLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSelected(position, "itemClick");

            }
        });

        if (isPatient) {
            mHolder.inputBtn.setVisibility(View.VISIBLE);
            mHolder.inputBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent intent = new Intent(mContext, MonitorInputActivity.class);
//                    intent.putExtra("monitorId", datas.get(position).getKindId());
//                    mContext.startActivity(intent);
                    listener.onSelected(position, "input");
                }
            });
        } else {
            mHolder.inputBtn.setVisibility(View.GONE);
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_chart1_layout, null);
        ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends ViewHolder {

        ImageView iconIV;
        TextView titleTV;
        TextView dateTV;
        LineChartView chartLayout;
        Button inputBtn;
        LinearLayout nodataLayout;

        ImageView nodataIV;
        TextView nodataTV;

        LinearLayout abnormalLayout;
        LinearLayout lowLayout;
        LinearLayout highLayout;


        public Holder(View view) {
            super(view);
            iconIV = (ImageView) view.findViewById(R.id.item_chart_icon);
            titleTV = (TextView) view.findViewById(R.id.item_chart_title);
            dateTV = (TextView) view.findViewById(R.id.item_chart_date);
            chartLayout = (LineChartView) view.findViewById(R.id.item_chart);

            inputBtn = (Button) view.findViewById(R.id.monitor_btn_input);
            nodataLayout = (LinearLayout) view.findViewById(R.id.chart_item_nodata);
            nodataTV = (TextView) view.findViewById(R.id.chart_item_nodata_tv);
            nodataIV = (ImageView) view.findViewById(R.id.chart_item_nodata_iv);

            abnormalLayout = (LinearLayout) view.findViewById(R.id.monitor_layout_abnormal);
            lowLayout = (LinearLayout) view.findViewById(R.id.monitor_layout_low);
            highLayout = (LinearLayout) view.findViewById(R.id.monitor_layout_high);


        }
    }

    public interface ResultSelectedListener {
        void onSelected(int position, String type);
    }

    private LineChartData data;
//    private LineChartView chart;

    private void generateData(String min, String max, List<PiontListBean> pointList, LineChartView chart, String isKindBean) {

        List<Line> lines = new ArrayList<Line>();
        for (int i = 0; i < pointList.size(); ++i) {
            List<PointValue> values = new ArrayList<PointValue>();
            if (pointList.get(i).getPointList() != null) {
                for (int j = 0; j < pointList.get(i).getPointList().size(); j++) {
                    PointBean bean = pointList.get(i).getPointList().get(j);
                    String[] times = bean.getTime().split(":");
                    int timepoint = Integer.parseInt(times[0]);
//                  System.out.println("response==timepoint=======" + timepoint);
                    PointValue point = new PointValue(timepoint, (int) Double.parseDouble(bean.getValue()));
                    Resources res = mContext.getResources();

                    if ("1".equals(isKindBean)) {
                        point.setColor("0");//紫色
                        point.setPurpliColor(mContext.getResources().getColor(R.color.purpli));
                        if ("0".equals(bean.getIsSpecial())) {
                            Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_purple_circle);
                            point.setBitmap(bmp);
                        } else {
                            point.setBig(true);
                            Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                            point.setBitmap(bmp);
                        }

                    } else if ("2".equals(isKindBean)) {
                        if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                            point.setColor("1");//深蓝
                            Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_blue_circle);
                            point.setBitmap(bmp);
                        } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                            point.setColor("2");//黄色
                            Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_yellow_circle);
                            point.setBitmap(bmp);
                        }
                        if ("0".equals(bean.getIsSpecial())) {
                            if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_blue_circle);
                                point.setBitmap(bmp);
                            } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_yellow_circle);
                                point.setBitmap(bmp);
                            }
                        } else {
                            point.setBig(true);
                            Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                            point.setBitmap(bmp);
                        }
                    } else {
                        if ("0".equals(bean.getIsSpecial())) {
                            Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_purple_circle);
                            point.setBitmap(bmp);
                        } else {
                            point.setBig(true);
                            Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.monitor_red_circle);
                            point.setBitmap(bmp);
                        }
                    }
                    values.add(point);
                }
                Line line = new Line(values);
                if ("1".equals(isKindBean)) {
                    line.setColor(mContext.getResources().getColor(R.color.purpli));
                } else if ("2".equals(isKindBean)) {
                    if (mContext.getResources().getString(R.string.high_value).equals(pointList.get(i).getName())) {
                        line.setColor(mContext.getResources().getColor(R.color.blue));
                    } else if (mContext.getResources().getString(R.string.low_value).equals(pointList.get(i).getName())) {
                        line.setColor(mContext.getResources().getColor(R.color.possible_result_points));
                    }
                } else {
                    line.setColor(mContext.getResources().getColor(R.color.blue));
                }


                lines.add(line);

            }
        }

        data = new LineChartData(lines);

        List<AxisValue> mAxisValues = new ArrayList<AxisValue>();
        mAxisValues.add(new AxisValue(0).setLabel("00:00")); //为每个对应的i设置相应的label(显示在X轴)
        mAxisValues.add(new AxisValue(1).setLabel("01:00"));
        mAxisValues.add(new AxisValue(2).setLabel("02:00"));
        mAxisValues.add(new AxisValue(3).setLabel("03:00"));
        mAxisValues.add(new AxisValue(4).setLabel("04:00"));
        mAxisValues.add(new AxisValue(5).setLabel("05:00"));
        mAxisValues.add(new AxisValue(6).setLabel("06:00"));
        mAxisValues.add(new AxisValue(7).setLabel("07:00"));
        mAxisValues.add(new AxisValue(8).setLabel("08:00"));
        mAxisValues.add(new AxisValue(9).setLabel("09:00"));
        mAxisValues.add(new AxisValue(10).setLabel("10:00"));
        mAxisValues.add(new AxisValue(11).setLabel("11:00"));
        mAxisValues.add(new AxisValue(12).setLabel("12:00"));
        mAxisValues.add(new AxisValue(13).setLabel("13:00"));
        mAxisValues.add(new AxisValue(14).setLabel("14:00"));
        mAxisValues.add(new AxisValue(15).setLabel("15:00"));
        mAxisValues.add(new AxisValue(16).setLabel("16:00"));
        mAxisValues.add(new AxisValue(17).setLabel("17:00"));
        mAxisValues.add(new AxisValue(18).setLabel("18:00"));
        mAxisValues.add(new AxisValue(19).setLabel("19:00"));
        mAxisValues.add(new AxisValue(20).setLabel("20:00"));
        mAxisValues.add(new AxisValue(21).setLabel("21:00"));
        mAxisValues.add(new AxisValue(22).setLabel("22:00"));
        mAxisValues.add(new AxisValue(23).setLabel("23:00"));
        mAxisValues.add(new AxisValue(24).setLabel("24:00"));


        Axis axisX = new Axis(mAxisValues);

        List<AxisValue> yAxisValues = new ArrayList<AxisValue>();
        int maxInt = 0;
        int minInt = 0;
        if (!"".equals(max)) {
            maxInt = (int) Double.parseDouble(max);
        }
        if (!"".equals(min)) {
            minInt = (int) Double.parseDouble(min);
        }


        int interValue = (int) Math.ceil(((double) (maxInt - minInt) / 5));
        yAxisValues.add(new AxisValue(interValue).setLabel(interValue + "")); //为每个对应的i设置相应的label(显示在X轴)
        yAxisValues.add(new AxisValue(interValue * 2).setLabel((interValue * 2) + ""));
        yAxisValues.add(new AxisValue(interValue * 3).setLabel((interValue * 3) + ""));
        yAxisValues.add(new AxisValue(interValue * 4).setLabel((interValue * 4) + ""));
        yAxisValues.add(new AxisValue(interValue * 5).setLabel((interValue * 5) + ""));

        Axis axisY = new Axis(yAxisValues).setHasLines(true);

        data.setAxisXBottom(axisX);
        data.setAxisYLeft(axisY);

        chart.setLineChartData(data);

        final Viewport v = new Viewport(chart.getMaximumViewport());
        v.bottom = 0;
        v.top = maxInt + maxInt / 10;
        v.left = 0;
        v.right = 30 - 1;
        chart.setMaximumViewport(v);
        chart.setCurrentViewport(v);

    }


}
